package com.umfb.config.zk;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.ConfigurablePropertyResolver;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;

/**
 * Created by zcah on 18-4-23.
 */
public class ZooKeeperPropertyPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer {

    private final ZooKeeperPropertySource zkPropertySource = new ZooKeeperPropertySource();

    private static final String UMFB_ZK_SOURCE_NAME="umfbProperties";

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        super.postProcessBeanFactory(beanFactory);
        MutablePropertySources propertySources = (MutablePropertySources)this.getAppliedPropertySources();

        propertySources.addLast(new PropertySource<ZooKeeperPropertySource>(UMFB_ZK_SOURCE_NAME, zkPropertySource) {
            @Override
            public String getProperty(String key) {
                return this.source.getProperty(key);
            }
        });
    }

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, ConfigurablePropertyResolver propertyResolver) throws BeansException {
        super.processProperties(beanFactoryToProcess, propertyResolver);
    }
}
